
from flask import Flask, render_template
import pymysql

app = Flask(__name__)

@app.route('/')
def index():
    # 连接数据库
    conn= pymysql.connect(host="localhost", user="root", password="123456", database="kq")
    cursor = conn.cursor()

    # 执行SQL查询语句，获取前三名学生
    query = "SELECT * FROM student ORDER BY gpa DESC LIMIT 3"
    cursor.execute(query)
    results = cursor.fetchall()

    # 关闭数据库连接
    cursor.close()
    conn.close()

    # 渲染HTML模板，并将查询结果传递给模板
    return render_template('4.html', students=results)

if __name__ == '__main__':
    app.run()